package com.itheima.userdemo.controller;

import com.itheima.userdemo.utils.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        // 生成验证码
        ServletOutputStream os = response.getOutputStream();
        // 这个工具有2给左右，一个是把图片给浏览器，另外就是返回给我们里面的验证码字符串
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, os, 4);
        // 这个方法执行的时候，客户端那边就拿到了图片的二进制代码
        // 把生成的验证码，放入session对象
        HttpSession httpSession = request.getSession();
        System.out.println("CheckCodeServlet checkCode:"+checkCode);
        httpSession.setAttribute("checkCode",checkCode);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}